import path from 'node:path'
import fs from 'fs-extra'
import chokidar from 'chokidar'
import {getStat} from 'recursive-readdir-files'
import {Options, run, createHTML, copyAttachment} from './utils/utils.js'

export default async function watch(options: Options) {
    await run(options)
    const homeMdPath = path.resolve(process.cwd(), 'README.md')
    const inputMDPath = path.resolve(process.cwd(), options.config.inputDir)
    const attachmentsPath = path.resolve(process.cwd(), options.config.attachmentDir)

    const watcher = chokidar.watch([homeMdPath, inputMDPath, attachmentsPath], {
        ignored: /(^|[\/\\])\../, // ignore dotfiles
        persistent: true,
    })

    watcher
        .on('change', async (filepath) => {
            console.log(`♻️ \x1b[32;1m modified file: ${path.relative(process.cwd(), filepath)} \x1b[0m`)
            // let stats = await getStat(filepath)
            // let relativePath = path.resolve(options.config.outputPath, path.relative(process.cwd(), filepath))
            // switch (true) {
            //     // 博客内容
            //     case filepath.startsWith(homeMdPath) || filepath.startsWith(inputMDPath):
            //         createHTML([stats], options)
            //         break
            //     // 静态资源
            //     case filepath.startsWith(attachmentsPath):
            //         await fs.copyFile(filepath, relativePath)
            //         break
            //     default:
            //         break
            // }
        })
        .on('add', async (filepath) => {
            console.log(`♻️ \x1b[32;1m add file: ${path.relative(process.cwd(), filepath)} \x1b[0m`)
            let stats = await getStat(filepath)
            let relativePath = path.resolve(options.config.outputPath, path.relative(process.cwd(), filepath))
            console.log(
                'filepath: ',
                filepath,
                '\nstats: ',
                stats,
                filepath.startsWith(homeMdPath) || filepath.startsWith(inputMDPath),
            )
            switch (true) {
                // 博客内容
                case filepath.startsWith(homeMdPath) || filepath.startsWith(inputMDPath):
                    createHTML([stats], options)
                    break
                // 附件
                case filepath.startsWith(attachmentsPath):
                    await fs.copyFile(filepath, relativePath)
                    break
                default:
                    break
            }
        })
        .on('unlink', async (filepath) => {
            console.log(`♻️ \x1b[32;1m remove file: ${path.relative(process.cwd(), filepath)} \x1b[0m`)
            let relativePath = path.resolve(options.config.outputPath, path.relative(process.cwd(), filepath))
            await fs.remove(relativePath)
        })
        .on('addDir', async (filepath) => {
            console.log(`♻️ \x1b[32;1m add dir: ${path.relative(process.cwd(), filepath)} \x1b[0m`)
            let relativePath = path.resolve(options.config.outputPath, path.relative(process.cwd(), filepath))
            await fs.ensureDir(relativePath)
        })
        .on('unlinkDir', async (filepath) => {
            console.log(`♻️ \x1b[32;1m remove dir: ${path.relative(process.cwd(), filepath)} \x1b[0m`)
            await fs.remove(filepath)
        })
        .on('error', (error) => console.log(`Watcher error: ${error}`))
}
